Inside Macintosh: Sound

| Previous | Chapter contents | Chapter top | Section top | Next |

Synchronizing Sound Channels

You can synchronize several different sound channels by issuing syncCmd commands. The param1 field of the sound command contains a count, and the param2 field contains an arbitrary identifier. The Sound Manager keeps track of the count for each channel being synchronized. When the Sound Manager receives a syncCmd command for a certain channel, it decrements the count for each channel having the given identifier, including the newly synchronized channel. Command processing resumes on a channel when the count becomes 0. Thus, if you know how many channels you need to synchronize, you can synchronize them all by arranging for all of their counts to become zero simultaneously. Listing 1-17 illustrates the use of the syncCmd command.

Listing 17 Adding a channel to a group of channels to be synchronized

PROCEDURE MySync1Chan (chan: SndChannelPtr; count: Integer;
                                identifier: LongInt);
VAR
    mySndCmd:       SndCommand;             {a sound command}
    myErr:          OSErr;
BEGIN
    WITH mySndCmd DO
    BEGIN
        cmd := syncCmd;                     {the command is syncCmd}
        param1 := count;
        param2 := identifier;               {ID of group to be synchronized}
    END;
    myErr := SndDoImmediate(chan, mySndCmd);
    IF myErr <> noErr THEN
        DoError(myErr);
END;

For example, to synchronize three channels, first create the channels and then call the MySync1Chan procedure defined in Listing 1-17 for the first channel with a count equal to 4, for the second channel with a count equal to 3, and for the third channel with a count equal to 2, using the same arbitrary identifier for each call to MySync1Chan . Then fill all channels with appropriate sound commands. (For example, you might send commands that will cause the same sequence of notes to be produced on all three synchronized channels.) Finally, call the MySync1Chan procedure one final time, passing any of the three channels and a count of 1. By that time, all of the other channels will have counts of 1, and all counts will become 0 simultaneously, thus initiating synchronized play.

The syncCmd command is intended to make it easy to synchronize sound channels. You can use the syncCmd command to start multiple channels of sampled sound playing simultaneously, but if you require precise synchronization of sampled-sound channels, you might achieve better results with the Time Manager, which is described in  Inside Macintosh: Processes .


© 1998 Apple Computer, Inc.

| Previous | Chapter contents | Chapter top | Section top | Next |